//
//  PassWordViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//
//修改密码
#import "PassWordViewController.h"
#import "AFNetworking.h"
#import "LoginViewController.h"
@interface PassWordViewController () <UITextFieldDelegate>{
    UITextField *_oldTextfiled;
    UITextField *_newTextfiled;
    UITextField *_ReTextfiled;
}
@property (nonatomic,strong)UITextField *textField;

@end

@implementation PassWordViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    self.navigationItem.title = @"修改密码";
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI{

    _oldTextfiled = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH *20/375, HEIGHT *36/667 + HEIGHT *0/667, WIDTH - WIDTH *40/375, HEIGHT *40/667)];
    _oldTextfiled.secureTextEntry = YES;
    _oldTextfiled.placeholder = @"请输入旧密码";
    [_oldTextfiled setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_oldTextfiled setValue:[UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH] forKeyPath:@"_placeholderLabel.font"];
    [_oldTextfiled setBorderStyle:UITextBorderStyleNone];
    [self.view addSubview:_oldTextfiled];
    _oldTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    _oldTextfiled.secureTextEntry = YES;
    _oldTextfiled.tag = 100;
    
    
    _newTextfiled = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH *20/375, HEIGHT *36/667 + HEIGHT *60/667, WIDTH - WIDTH *40/375, HEIGHT *40/667)];
    _newTextfiled.secureTextEntry = YES;
    _newTextfiled.placeholder = @"请输入新密码";
    [_newTextfiled setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_newTextfiled setValue:[UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH] forKeyPath:@"_placeholderLabel.font"];
    [_newTextfiled setBorderStyle:UITextBorderStyleNone];
    [self.view addSubview:_newTextfiled];
    _newTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    _newTextfiled.secureTextEntry = YES;
    _newTextfiled.tag = 101;
    
    
    _ReTextfiled = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH *20/375, HEIGHT *36/667 + HEIGHT *120/667, WIDTH - WIDTH *40/375, HEIGHT *40/667)];
    _ReTextfiled.secureTextEntry = YES;
    _ReTextfiled.placeholder = @"请再次输入新密码";
    [_ReTextfiled setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_ReTextfiled setValue:[UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH] forKeyPath:@"_placeholderLabel.font"];
    [_ReTextfiled setBorderStyle:UITextBorderStyleNone];
    [self.view addSubview:_ReTextfiled];
    _ReTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    _ReTextfiled.secureTextEntry = YES;
    _ReTextfiled.tag = 102;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textFieldInput)];
    [self.view addGestureRecognizer:tap];
    
    [self.view addSubview:_textField];
    //提交按钮
    UIButton *submitBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH *40/375, HEIGHT *272/667, WIDTH - WIDTH *80/375, HEIGHT *40/667)];
    submitBtn.backgroundColor = UIColorFromRGB(0xff6a6a);
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submitBtn.titleLabel.font = [UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH];
    [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    submitBtn.layer.cornerRadius = 5;
    
    UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH *20/375, HEIGHT *206/667, WIDTH - WIDTH *20/375, HEIGHT *20/667)];
    tipLabel.text = @"6-20位，建议数字、字母、符合组合。";
    [self.view addSubview:tipLabel];
    tipLabel.textColor = UIColorFromRGB(0x333333);
    tipLabel.font = [UIFont fontWithName:@"Heiti SC" size:13 *MYWIDTH];
}
-(void)submitClick{
    NSLog(@"提交点击");
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if (_newTextfiled.text.length < 6 || _ReTextfiled.text.length < 6) {
        [self.view makeToast:@"新密码长度小于6位" duration:1.5 position:CSToastPositionCenter];
    }else if (![_ReTextfiled.text isEqualToString:_newTextfiled.text]){
        [self.view makeToast:@"新密码输入不一致" duration:1.5 position:CSToastPositionCenter];
    }else if (![_oldTextfiled.text isEqualToString:[user objectForKey:@"password"]]){
        [self.view makeToast:@"旧密码输入错误" duration:1.5 position:CSToastPositionCenter];
    }else if( [_newTextfiled.text isEqualToString:[user objectForKey:@"password"]]){
        [self.view makeToast:@"新旧密码不能相同" duration:1.5 position:CSToastPositionCenter];
    }else{
        // NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *urlstr = @"http://hmyx.ijiaque.com/app/user/edit_pwd.html";
        
        NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                              @"old_password":_oldTextfiled.text,
                              @"new_password":_newTextfiled.text,
                              @"re_password":_ReTextfiled.text};
        
        AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
        manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [manager POST:urlstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if (![[dic1 objectForKey:@"status"] isEqual:@"1"]) {
                
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:@"" forKey:@"title"];
                [user setObject:@"" forKey:@"name"];
                [user setObject:@"" forKey:@"token"];
                [user setObject:@"" forKey:@"imageurl"];
                [user setObject:@"" forKey:@"birthday"];
                [user setObject:@"" forKey:@"mail"];
                
                [user synchronize];
                
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"密码修改成功，请重新登录" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                //获取UserDefaults单例
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                //移除UserDefaults中存储的用户信息
                [userDefaults removeObjectForKey:@"name"];
                [userDefaults removeObjectForKey:@"password"];
                [userDefaults removeObjectForKey:@"token"];
                [userDefaults removeObjectForKey:@"nickname"];
                [userDefaults removeObjectForKey:@"mobile"];
                [userDefaults synchronize];
                
         //       [self cancelJpush];
                
                LoginViewController * loginVC =[[LoginViewController alloc]init];
                [self presentViewController:loginVC animated:YES completion:nil];
                
            }
            
       //     [MBProgressHUD hideHUDForView:self.view animated:YES];
            
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            
       //     [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.view makeToast:@"修改失败，请检查网络" duration:1.5 position:CSToastPositionCenter];
            
        }];
    }
    

    
    
}
-(void)textFieldInput{
     UITextField *textField = (UITextField *)[self.view viewWithTag:100];
    [textField resignFirstResponder];
    
     UITextField *textField1 = (UITextField *)[self.view viewWithTag:101];
    [textField1 resignFirstResponder];
    
     UITextField *textField2 = (UITextField *)[self.view viewWithTag:102];
    [textField2 resignFirstResponder];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
